package com.mo.system.permission.control;

import com.mo.global.model.system.Module;
import com.mo.global.model.system.Staff;
import com.mo.global.utils.LoginToken;
import com.mo.global.utils.ResultBody;
import com.mo.system.permission.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Auther:Lang
 * @Date:2022-12-08
 * @Time:14:10
 * @Description:TODO
 */
@RestController
@RequestMapping("/system/permission")
public class PermissionAction {
    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/login")
    public ResultBody login(String username, String password) {
        Staff staff = permissionService.loginCheck(username, password);
        if (staff == null) {
            ResultBody resultBody = new ResultBody();
            resultBody.setCode(401);
            resultBody.setMessage("账户或密码错误！");
            return resultBody;
        }
        //判断账户是否已登录
        Map<String, LoginToken> loginToken = (Map<String, LoginToken>) redisTemplate.opsForValue().get("loginToken");
        //如果为空，则创建新的集合
        if (loginToken == null)
            loginToken = new HashMap<>();

        LoginToken token = loginToken.get(String.valueOf(staff.getId()));

        //如果账户已登录
        if (token != null) {
            ResultBody resultBody = new ResultBody();
            resultBody.setCode(402);
            resultBody.setMessage("该账号已登录");
            return resultBody;
        }

        //未登录则读取员工信息
        LoginToken newToken = new LoginToken();
        newToken.setStaff(staff);
        newToken.setToken(String.valueOf(UUID.randomUUID()));
        newToken.setJob(permissionService.queryJobByStaff(staff));
        newToken.setRole(permissionService.queryRoleByStaff(staff));
        newToken.setModules(permissionService.queryModuleByStaff(staff));

        //将token 存进redis
        loginToken.put(String.valueOf(staff.getId()), newToken);

        redisTemplate.opsForValue().set("loginToken", loginToken);
        ResultBody resultBody = new ResultBody();
        resultBody.setCode(200);
        resultBody.setMessage("登录成功");
        resultBody.setData(newToken);
        return resultBody;
    }

    @RequestMapping("/loginOut")
    public ResultBody loginOut( String id) {
        //根据键获取 redis
        Map<String, LoginToken> loginToken = (Map<String, LoginToken>) redisTemplate.opsForValue().get("loginToken");
        if (loginToken == null) loginToken = new HashMap<>();
        LoginToken token = loginToken.get(id);
        if (token != null) {
            loginToken.remove(id);
        }
        redisTemplate.opsForValue().set("loginToken", loginToken);
        return new ResultBody(200, "退出成功", null);
    }


    @RequestMapping("/module")
    public List<List<Module>> getModules(Staff staff) {
        return permissionService.queryModuleByStaff(staff);
    }

    @RequestMapping("/checkStaff")  //每次访问数据都会检查一下是否登录
    public ResultBody checkStaff(@RequestHeader(required = false) String id,
                                 @RequestHeader(value = "token", required = false) String strToken) {
        //当请求头里不含 员工id，token时为未登录
        if (id == null || strToken == null) {
            return new ResultBody(401, "未登录", null);
        }
        //从reids 缓存里拿出键为 loginToken 的数据
        Map<String, LoginToken> loginToken = (Map<String, LoginToken>) redisTemplate.opsForValue().get("loginToken");
        if (loginToken == null) {
            loginToken = new HashMap<>();
        }
        // 当请求头里带有员工id，token时需要进行验证
        LoginToken token = loginToken.get(id);
        //判断token是否为空或者是否和缓存里的是否一致
        if (token == null || !strToken.equals(token.getToken())) {
            return new ResultBody(401, "未登录", null);
        }
        return new ResultBody(200, "已登录", null);
    }
}
